/*
 * Copyright 2010 dev7.jp
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.dev7.android.util;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class Views {

    private Views() {
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T findViewById(View view, int id) {
        return (T) view.findViewById(id);
    }

    public static void setClickListnerToButton(View view, int id,
            OnClickListener onClickListener) {
        final Button button = findViewById(view, id);
        button.setOnClickListener(onClickListener);
    }

    public static void setText(View view, int id, CharSequence text) {
        final TextView v = findViewById(view, id);
        v.setText(text);
    }

    public static void setVisible(View view, int id, boolean visible) {
        final View v = findViewById(view, id);
        if (visible) {
            v.setVisibility(View.VISIBLE);
        } else {
            v.setVisibility(View.INVISIBLE);
        }
    }

    public static void setTextToChild(ViewGroup input, int i, String name) {
        TextView t = (TextView) input.getChildAt(i);
        t.setText(name);
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T getChildAt(ViewGroup viewGroup, int i) {
        return (T) viewGroup.getChildAt(i);
    }

}
